﻿using System.Data;
using System.Data.SqlClient;
using System;

namespace Adams.Web.Areas.MyAdams.Data.Profile
{
    public static class General
    {
        public static Adams.Web.Areas.MyAdams.Models.Profile.General.PageLoad PageLoad(int memberId)
        {
            var data = new Adams.Web.Areas.MyAdams.Models.Profile.General.PageLoad();

            using (SqlDataReader dr = Adams.Web.Data.DB.RunDataReader(@"
SELECT M.*
FROM Membership.Members M
WHERE M.MemberID = @MemberID
", CommandType.Text,
    Adams.Web.Data.DB.CreateParameter("@MemberID", memberId)))
            {
                if (dr.HasRows)
                {
                    dr.Read();
                    data.Member = Adams.Web.Data.DB.InitFromReader<Adams.Shared.Entities.Membership.Member>(dr, new Adams.Web.Data.ColMap { NameInDb = "MemberID", NameInObject = "ID" });
                }
            }

            return data;
        }

        public static void SaveGeneral(int memberId, MyAdams.Models.Profile.General.FormInput input)
        {
            Adams.Web.Data.DB.Run(@"
UPDATE Membership.Members SET
    FirstName = @FirstName
    ,LastName = @LastName
    ,Street = @Street
    ,Street2 = @Street2
    ,City = @City
    ,State = @State
    ,ZipCode = @ZipCode
    ,Phone = @Phone
WHERE MemberID = @MemberID
", CommandType.Text,
                    Adams.Web.Data.DB.CreateParameter("@MemberID", memberId),
                    Adams.Web.Data.DB.CreateParameter("@FirstName", input.FirstName),
                    Adams.Web.Data.DB.CreateParameter("@LastName", input.LastName),
                    Adams.Web.Data.DB.CreateParameter("@Street", input.Street),
                    Adams.Web.Data.DB.CreateParameter("@Street2", input.Street2.ToNString()),
                    Adams.Web.Data.DB.CreateParameter("@City", input.City),
                    Adams.Web.Data.DB.CreateParameter("@State", input.State),
                    Adams.Web.Data.DB.CreateParameter("@ZipCode", input.ZipCode),
                    Adams.Web.Data.DB.CreateParameter("@Phone", input.Phone));
        }
    }
}
